home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / dev / basic / ace24dist.lha / ace24.lha / utils / ab2ascii-1.3 / codelines.c < prev    next >
C/C++ Source or Header  |  1996-09-11  |  3KB  |  111 lines

  1. /*
  2.  *  CODELINES.C
  3.  */
  4.  
  5. /*
  6.  * (c)Copyright 1994 by Tobias Ferber.
  7.  *
  8.  * This file is part of AmigaBASIC->ASCII.
  9.  *
  10.  * AmigaBASIC->ASCII is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU General Public License as
  12.  * published by the Free Software Foundation; either version 1 of the
  13.  * License, or (at your option) any later version.
  14.  *
  15.  * AmigaBASIC->ASCII is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with AmigaBASIC->ASCII; see the file COPYING.  If not, write to
  22.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  */
  24.  
  25. #include "abasic.h"
  26.  
  27. /*
  28.    Allocate and read one line of code and link it to the global code_buffer.
  29.    The fread() function will be used with `len' packets size of size 1 because
  30.    fread() returns the number of *complete* packets read.  This way we can also
  31.    handle partial packets if needed.
  32.    The number of 1 byte packets read will be returned.  If this value is != len
  33.    then this indicates an error.
  34. */
  35.  
  36.  
  37. static codeline_t *cl_tail= NIL(codeline_t *);
  38.  
  39. int
  40. read_line( fp, len, flags )
  41. FILE *fp;
  42. int len, flags;
  43. {
  44.   codeline_t *cl;
  45.   int n= 0;
  46.  
  47.   if( cl= (codeline_t *)malloc(CODELINE_SIZE) )
  48.   {
  49.     unsigned char *s= (unsigned char *)malloc( (len+1) * sizeof(unsigned char) );
  50.  
  51.     if( s )
  52.     {
  53.       cl->s= s;
  54.       cl->len= len;
  55.       cl->flags= flags;
  56.  
  57.       cl->next= NIL(codeline_t *);
  58.  
  59.       if(cl_tail)  cl_tail->next= cl;
  60.       else         code_buffer= cl;
  61.  
  62.       cl_tail= cl;
  63.  
  64.       /*for( n=0; n<len && !feof(fp); s[n++]= fgetc(fp) );*/
  65.  
  66.       n= (int)fread(s, 1L, len, fp);
  67.       s[len]= '\0';
  68.  
  69.       ++numlines;
  70.     }
  71.     else
  72.     {
  73.       free(cl);
  74.       cl= NIL(codeline_t *);
  75.     }
  76.   }
  77.   /* else panic! */
  78.  
  79.   return cl ? n:0;
  80. }
  81.  
  82.  
  83. /*
  84.    Free up all lines of code linked to the global code_buffer.  Normally all
  85.    code lines are free()d up immediately after they have been expanded by
  86.    expand_code() but in fact we need it for two reasons:
  87.  
  88.    (1) to purge the code area (e.g. in case of a fatal error)
  89.    (2) to reset cl_tail!  This means you *MUST* call free_code() even if
  90.        code_buffer is empty!
  91. */
  92.  
  93. void
  94. free_code( void )
  95. {
  96.   codeline_t *cl= code_buffer;
  97.  
  98.   while(cl)
  99.   {
  100.     codeline_t *t= cl;
  101.     cl= cl->next;
  102.  
  103.     if(t->s)
  104.       free(t->s);
  105.  
  106.     free(t);
  107.   }
  108.  
  109.   code_buffer= cl_tail= NIL(codeline_t *);
  110. }
  111.